package com.example.lin.day9_12;

import android.Manifest;
import android.app.AliasActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

public class ALiPayActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        permissionRequest();

        setContentView(R.layout.activity_ali_pay);
    }


    private static final int PERMISSION_REQUEST_CODE = 654;

    private void permissionRequest() {
        ArrayList<String> permissionList = new ArrayList<>();
        int callPhoneGrant = ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if (callPhoneGrant != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.CALL_PHONE);
        }
        int storagePhoneGrant = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (storagePhoneGrant != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (permissionList.size() > 0) {
            //证明有权限还没有被授予
            //请求权限
            String[] array = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(this, array, PERMISSION_REQUEST_CODE);
        } else {
            Toast.makeText(this, "可以搞事情了...", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0) {
                for (int i = 0; i < grantResults.length; i++) {
                    if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                            new AlertDialog.Builder(ALiPayActivity.this)
                                    .setTitle("权限申请")
                                    .setCancelable(false)
                                    .setMessage("支付需要存储权限才可以运行...")
                                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Intent intent = new Intent();
                                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                                            intent.setData(uri);
                                            startActivity(intent);
                                            finish();
                                        }
                                    }).setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            }).show();
                        }
                    }
//                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//                        Log.d("ABC", permissions[i] + " 被授予了");
//                    } else {
//                        Log.w("ABC", permissions[i] + " 被拒绝了");
//                    }
                }
                //TODO 一些其他的逻辑,,根据项目的需求


            }


        }
    }
}
